package com.server;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sample.Instrument;

public class SpringService extends HttpServlet{
	private static final long serialVersionUID = 9177609475590898611L;
	
	private WebApplicationContext _springContext = null;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		_springContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Object obj = _springContext.getBean("instrumentbean");
		
		if(obj != null && obj instanceof Instrument){
			System.out.println(((Instrument)obj).getMusicalInstrument());
		}
	}
}
